package com.castle.fortress.admin.study.course.service.impl;

import cn.hutool.core.util.StrUtil;
import com.castle.fortress.admin.study.course.dto.StudyCoursewareDto;
import com.castle.fortress.admin.study.course.service.StudyCoursewareService;
import com.castle.fortress.admin.utils.BizCommonUtil;
import com.castle.fortress.common.utils.ConvertUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.castle.fortress.admin.study.course.entity.StudyCourseChapterEntity;
import com.castle.fortress.admin.study.course.dto.StudyCourseChapterDto;
import com.castle.fortress.admin.study.course.mapper.StudyCourseChapterMapper;
import com.castle.fortress.admin.study.course.service.StudyCourseChapterService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import java.util.List;

/**
 * 课程章节表 服务实现类
 *
 * @author castle
 * @since 2023-01-31
 */
@Service
public class StudyCourseChapterServiceImpl extends ServiceImpl<StudyCourseChapterMapper, StudyCourseChapterEntity> implements StudyCourseChapterService {
    @Autowired
    private StudyCoursewareService studyCoursewareService;

    /**
    * 获取查询条件
    * @param studyCourseChapterDto
    * @return
    */
    public QueryWrapper<StudyCourseChapterEntity> getWrapper(StudyCourseChapterDto studyCourseChapterDto){
        QueryWrapper<StudyCourseChapterEntity> wrapper= new QueryWrapper();
        wrapper.orderByAsc("sort");
        if(studyCourseChapterDto != null){
            StudyCourseChapterEntity studyCourseChapterEntity = ConvertUtil.transformObj(studyCourseChapterDto,StudyCourseChapterEntity.class);
            wrapper.eq(studyCourseChapterEntity.getId() != null,"id",studyCourseChapterEntity.getId());
            wrapper.eq(studyCourseChapterEntity.getCourseId() != null,"course_id",studyCourseChapterEntity.getCourseId());
            wrapper.like(StrUtil.isNotEmpty(studyCourseChapterEntity.getChapterName()),"chapter_name",studyCourseChapterEntity.getChapterName());
            wrapper.eq(studyCourseChapterEntity.getSort() != null,"sort",studyCourseChapterEntity.getSort());
            wrapper.eq(studyCourseChapterEntity.getStatus() != null,"status",studyCourseChapterEntity.getStatus());
            wrapper.eq(studyCourseChapterEntity.getCreateUser() != null,"create_user",studyCourseChapterEntity.getCreateUser());
            wrapper.eq(studyCourseChapterEntity.getCreateTime() != null,"create_time",studyCourseChapterEntity.getCreateTime());
            wrapper.eq(studyCourseChapterEntity.getUpdateUser() != null,"update_user",studyCourseChapterEntity.getUpdateUser());
            wrapper.eq(studyCourseChapterEntity.getUpdateTime() != null,"update_time",studyCourseChapterEntity.getUpdateTime());
            wrapper.eq(studyCourseChapterEntity.getIsDeleted() != null,"is_deleted",studyCourseChapterEntity.getIsDeleted());
            wrapper.orderByDesc("create_time");
        }
        return wrapper;
    }


    @Override
    public IPage<StudyCourseChapterDto> pageStudyCourseChapter(Page<StudyCourseChapterDto> page, StudyCourseChapterDto studyCourseChapterDto) {
        QueryWrapper<StudyCourseChapterEntity> wrapper = getWrapper(studyCourseChapterDto);
        Page<StudyCourseChapterEntity> pageEntity = new Page(page.getCurrent(), page.getSize());
        Page<StudyCourseChapterEntity> studyCourseChapterPage=baseMapper.selectPage(pageEntity,wrapper);
        Page<StudyCourseChapterDto> pageDto = new Page(studyCourseChapterPage.getCurrent(), studyCourseChapterPage.getSize(),studyCourseChapterPage.getTotal());
        pageDto.setRecords(ConvertUtil.transformObjList(studyCourseChapterPage.getRecords(),StudyCourseChapterDto.class));
        return pageDto;
    }

    @Override
    public IPage<StudyCourseChapterDto> pageStudyCourseChapterExtends(Page<StudyCourseChapterDto> page, StudyCourseChapterDto studyCourseChapterDto){
        Map<String,Long> pageMap = BizCommonUtil.getPageParam(page);
        StudyCourseChapterEntity studyCourseChapterEntity = ConvertUtil.transformObj(studyCourseChapterDto,StudyCourseChapterEntity.class);
        List<StudyCourseChapterEntity> studyCourseChapterList=baseMapper.extendsList(pageMap,studyCourseChapterEntity);
        Long total = baseMapper.extendsCount(studyCourseChapterEntity);
        Page<StudyCourseChapterDto> pageDto = new Page(page.getCurrent(), page.getSize(),total);
        pageDto.setRecords(ConvertUtil.transformObjList(studyCourseChapterList,StudyCourseChapterDto.class));
        return pageDto;
    }
    @Override
    public StudyCourseChapterDto getByIdExtends(Long id){
        StudyCourseChapterEntity  studyCourseChapterEntity = baseMapper.getByIdExtends(id);
        return ConvertUtil.transformObj(studyCourseChapterEntity,StudyCourseChapterDto.class);
    }

    @Override
    public List<StudyCourseChapterDto> listStudyCourseChapter(StudyCourseChapterDto studyCourseChapterDto){
        QueryWrapper<StudyCourseChapterEntity> wrapper = getWrapper(studyCourseChapterDto);
        List<StudyCourseChapterEntity> list = baseMapper.selectList(wrapper);
        return ConvertUtil.transformObjList(list,StudyCourseChapterDto.class);
    }

    @Override
    public void delByCourseId(Long courseId) {
        if(courseId!=null){
            baseMapper.delByCourseId(courseId);
        }
    }

    @Override
    public void delByCourseIds(List<Long> courseIds) {
        if(courseIds!=null && courseIds.size()>0){
            baseMapper.delByCourseIds(courseIds);
        }
    }

    @Override
    public List<StudyCourseChapterDto> getListByCourseId(Long courseId) {
        StudyCourseChapterDto chapterQuery = new StudyCourseChapterDto();
        chapterQuery.setCourseId(courseId);
        List<StudyCourseChapterDto>  list= listStudyCourseChapter(chapterQuery);
        List<StudyCoursewareDto>  list2= studyCoursewareService.selectBycourseId(courseId,null);
        Map<Long,List<StudyCoursewareDto>> coursewareMap = new HashMap<>();
        for (StudyCoursewareDto dto:list2){
            List<StudyCoursewareDto> coursewareDtos = null;
            if(coursewareMap.get(dto.getChapterId()) ==null){
                coursewareDtos = new ArrayList<>();
            }else{
                coursewareDtos = coursewareMap.get(dto.getChapterId());
            }
            coursewareDtos.add(dto);
            coursewareMap.put(dto.getChapterId(),coursewareDtos);
        }
        for(StudyCourseChapterDto dto:list){
            dto.setCoursewares(coursewareMap.get(dto.getId()));
        }
        return list;
    }

    @Override
    public List<StudyCourseChapterDto> getStudyListByCourseId(Long id, Long memberId) {
        StudyCourseChapterDto chapterQuery = new StudyCourseChapterDto();
        chapterQuery.setCourseId(id);
        List<StudyCourseChapterDto>  list= listStudyCourseChapter(chapterQuery);
        List<StudyCoursewareDto>  list2= studyCoursewareService.selectStudyListBycourseId(id,null,memberId);
        Map<Long,List<StudyCoursewareDto>> coursewareMap = new HashMap<>();
        for (StudyCoursewareDto dto:list2){
            List<StudyCoursewareDto> coursewareDtos = null;
            if(coursewareMap.get(dto.getChapterId()) ==null){
                coursewareDtos = new ArrayList<>();
            }else{
                coursewareDtos = coursewareMap.get(dto.getChapterId());
            }
            coursewareDtos.add(dto);
            coursewareMap.put(dto.getChapterId(),coursewareDtos);
        }
        for(StudyCourseChapterDto dto:list){
            dto.setCoursewares(coursewareMap.get(dto.getId()));
        }
        return list;
    }
}

